﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Geodatabase;
using Akiha.Core;
using ESRI.ArcGIS.Geometry;
using System.Collections;


namespace ArcMap_AddIn_Tools.FeatureClass.UpdateDjzqbmNew
{


    public partial class CustomDblCombo : UserControl
    {

        //初始化传递的map对象
        private IMap m_pMap = null;

        
        //两个label的值
        private string strFirstName = "图层名称";
        private string strSecondName = "字段名称";

        //最终选择的字段值
        private IField m_pSelField = null;
        //当前选择的图层要素
        private IFeatureClass m_pSelFeaCls = null;

        public string FirstLable
        {
            get
            {
                return strFirstName;
 
            }
            set
            {
                strFirstName = value;
            }
        }

        public string SecondLable
        {
            get
            {
                return strSecondName;
            }
            set
            {
                strSecondName = value;
            }

        }

        public IFeatureClass SelFeatureClass { 
            get
            {
                return m_pSelFeaCls; 
            } 
        }

        public IField SelField { 
            get 
            {            
                return m_pSelField;
            } 
        }

        public IMap Map { get { return m_pMap; } set { m_pMap = value; } }






        public CustomDblCombo()
        {
            InitializeComponent();
        }

        //重载的构造函数
        public CustomDblCombo(IMap pMap)
        {
            m_pMap = pMap;
            InitializeComponent();
        }



      

        //修改map，lable后刷新控件
        public void RefreshControls()
        {
            lblFields.Text = strSecondName;
            lblLyr.Text = strFirstName;
            LoadFeatureLayerToCombox();
        }
        

        //载入图层的要素类，意味着暂时只支持要素类
        private void LoadFeatureLayerToCombox()
        {
            if (m_pMap == null)
                return;
            if (m_pMap.LayerCount == 0)
                return;
            List<IFeatureLayer> fcs = MapWrapper.GetLayers(m_pMap, null);
            
            if (fcs.Count == 0)
                return;


            cboLyr.ValueMember = "Value";
            cboLyr.DisplayMember = "Key";


            //一般图层数量较少可以考虑不使用update
            this.cboLyr.BeginUpdate();
           
            //添加键值对到cbobox中，键代表名称，值代表FeaCls的引用
            
            
            
            foreach (IFeatureLayer pLyr in fcs)
            {
                cboLyr.Items.Add(new DictionaryEntry(pLyr.Name, pLyr.FeatureClass));
            }
            

            this.cboLyr.EndUpdate();
            
            
        }

        private void LoadFieldsToCombox(IFeatureClass pFeaCls)
        {


            cboFields.Items.Clear();

            IFields fields = pFeaCls.Fields;
            cboFields.ValueMember = "Value";
            cboFields.DisplayMember = "Key";

            this.cboFields.BeginUpdate();
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                //对可编辑的字段进行赋值
                if (field.Editable == true)
                {
                    //选择别名
                    cboFields.Items.Add( new DictionaryEntry(field.AliasName,field));
                }
            }
            this.cboFields.EndUpdate();
            
        }

        private void CustomDblCombo_Load(object sender, EventArgs e)
        {
            RefreshControls();
        }

        private void cboLyr_SelectedIndexChanged(object sender, EventArgs e)
        {
            //这里涉及到拆装箱操作,

            m_pSelFeaCls = (IFeatureClass)(((DictionaryEntry)cboLyr.SelectedItem).Value);

            if (m_pSelFeaCls == null)
                return;
            LoadFieldsToCombox(m_pSelFeaCls);
            
        }

        private void cboFields_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_pSelField = (IField)(((DictionaryEntry)cboFields.SelectedItem).Value);
        }




    }
}
